/**
 * 在线用户
 * @author yuan_rz
 * @date 2019/12/12 09:34
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        vueData = {
            dataList: []
        },
        table_where = {},
        cols = [[
            {field: 'ID', width: 150, title: '会话ID', hide: 'true'}
            , {field: 'USER_NAME', width: 150, title: '登录用户'}
            , {field: 'HOST', width: 150, title: '登录IP'}
            , {field: 'LAST_ACCESS_TIME', width: 150, title: '最后操作时间'}
            , {field: 'START_TIMESTAMP', width: 150, title: '登录时间'}
            , {field: 'TIME_OUT', width: 150, title: '过期时间(毫秒)', hide: 'true'}
            , {
                width: 60,
                title: '',
                toolbar: '#script-onlineusersetting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        onlineuserSettingAction,
        moduleName = '在线用户';
    ;

    var onlineuser = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'onlineuser'
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'onlineuser_table'),
                    title: moduleName + '表',
                    id: 'onlineuser_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],
                    height: 'full',
                    cols: cols,
                    data: vueData.dataList,
                    loading: false,
                    page: true,
                    done: function (res, curr, count) {

                    }
                });

                layui.laytpl(app.getByid(doc, 'script-onlineusersetting-action').innerHTML).render({}, function (string) {
                    onlineuserSettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(onlineuser_table)', function (obj) {
                    switch (obj.event) {
                        case 'detail':
                            layer.tips(onlineuserSettingAction, obj.td, {
                                tips: [3, '']
                                , time: 5000
                                , anim: 500
                                , maxWidth: 120
                                , skin: 'layui-table-tips'
                                , success: function (layero, index) {
                                    tipsIndex = index;
                                    layero.find('li').on('click', function () {
                                        switch ($(this).attr('inx')) {
                                            case '1':
                                                self.killUser(obj);
                                                break;
                                        }
                                        layer.close(index);
                                        tipsIndex = null;
                                    });
                                }
                            });
                            return false;
                            break;
                    }
                });

                //监听事件
                table.on('toolbar(onlineuser_table)', function (obj) {
                    switch (obj.event) {
                        case 'refresh':
                            self.reloadTable();
                            break;
                    }
                });
            });
        },
        reloadTable: function () {
            app.ajax({
                xurl: 'sys/onlineuser/getListData',
                xdata: {},
                loading: true,
                cb: function (result) {
                    if (result.ok) {
                        vueData.dataList = result.data.data;
                        self._reloadTable({id: 'onlineuser_table', data: vueData.dataList});
                    }
                }
            });
        },
        killUser: function (obj) {
            app.ajax({
                xurl: 'sys/onlineuser/killUser',
                xdata: {sessionId: obj.data.ID},
                loading: true,
                confirm: {
                    msg: '强制下线?'
                },
                okmsg: moduleName + '强制下线成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTable();
                    }
                }
            });
        },
        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    methods: {},
                    created: function(){
                        self.reloadTable();
                    }
                }]
            });

            self.initTable();
        },
        _lazy: function () {
            this._super();
        }
    });

    return new onlineuser();

});